﻿using System.Collections.Specialized;
using System.Linq;
using System.Web;
using YuYuKiCms.Bl.Interface;
using YuYuKiCms.Dal.Interface;

namespace YuYuKiCms.Bl.Core
{
    internal class HomeBL : IHomeBL
    {
        private readonly IThemeRepository _themeRepository;

        public HomeBL(IThemeRepository themeRepository)
        {
            _themeRepository = themeRepository;
        }

        public string GetLanguage(HttpRequestBase request, StringCollection languages)
        {
            var lg = string.Empty;

            if (null != request.Cookies["lg"])
            {
                lg = request.Cookies["lg"].Value;
            }
            else
            {
                if (request.UserLanguages != null)
                    lg =
                        request.UserLanguages.AsParallel()
                            .FirstOrDefault(lang => languages.Contains(lang.Substring(0, 2)));

                if (string.IsNullOrWhiteSpace(lg))
                    lg = languages[0];
            }

            return lg;
        }
    }
}